/*
 * @Description  : 路由入口
 * @Autor        : 杜康
 * @Date         : 2023-02-23 14:27:53 +0800
 * @LastEditors  : 杜康
 * @LastEditTime : 2023-03-16 14:04:19 +0800
 */
import { createRouter, createWebHistory } from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import adminMenu from "@/assets/menus/adminMenu";
import { checkMenuToBreadCrumb } from "@/utils/utils";
import container from "@/router/container";
import Login from "@/views/login/Index.vue";
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/login"
    },
    {
      path: "/login",
      name: "Login",
      component: Login
    },
    ...container
  ]
});
const titleTpl = " | 后台管理";
router.beforeEach((to, from, next) => {
  NProgress.start();
  const bcObj = {};
  const bcLists = checkMenuToBreadCrumb(adminMenu, to.path, [], bcObj);
  const checkMenu = bcLists.breadCrumbList.slice(
    bcLists.breadCrumbList.length - 1,
    bcLists.breadCrumbList.length
  )[0];
  if (to.path === "/login") {
    document.title = `登录${titleTpl}`;
  } else if (to.path === "/401") {
    document.title = `401${titleTpl}`;
  } else if (to.path === "/404") {
    document.title = `404${titleTpl}`;
  } else {
    document.title = `${to?.name}${titleTpl}`;
  }
  const role = localStorage.getItem("role");
  const urole = to.meta && to.meta.role;
  if ((role && role === urole) || to.path === "/login" || !urole) {
    next();
  } else {
    alert("没有权限");
    next(false);
  }
});

router.afterEach((to, from) => {
  NProgress.done();
});

export default router;
